
# load packages
library(tidyverse)
library(haven)
library(blme)
library(rstanarm); options(mc.cores = parallel::detectCores())
library(tidybayes)


# load data
raw_df <- read_dta("data/cleaned GTE data.dta") %>%
  glimpse() 

aware_df <- raw_df %>%
  filter(primarymodule == 0) %>%
  select(starts_with("aware"), respid) %>%
  gather(issue_id, response, starts_with("aware")) %>%
  mutate(issue_id = str_replace(issue_id, "aware", "Issue ")) %>%
  drop_na() %>%
  glimpse()

fit <- bglmer(response ~ (1 | issue_id), data = aware_df, family = binomial)

stan_fit <- stan_glmer(response ~ (1 | issue_id), data = aware_df, family = binomial)

pred_df <- data.frame(issue_id = paste("Issue", 1:48)) %>%
  add_epred_draws(stan_fit) %>%
  group_by(issue_id) %>%
  summarize(awareness_stan = mean(.epred)) %>%
  mutate(awareness = predict(fit, newdata = ., type = "response")) %>%
  left_join(read_csv("output/issue-meta.csv")) %>%
  mutate(stem = reorder(stem, awareness_stan)) %>%
  write_csv("output/issue-meta-w-awareness.csv") %>%
  glimpse()

ggplot(pred_df, aes(x = awareness, y = stem)) + 
  geom_point() + 
  geom_point(aes(x = awareness_stan), color = "red")
